package cn.org.wifit.userserver.zookeeper.handle;

import io.netty.channel.ChannelHandler.Sharable;
import cn.org.wifit.core.zookeeper.NodeWriteDistributedTransactionWatcherHandle;
import cn.org.wifit.userserver.zookeeper.manager.ZookeeperIMUserWriteManager;

/**
 * @author yinwenjie
 */
@Sharable
public class IMUserBootWatcherHandle implements NodeWriteDistributedTransactionWatcherHandle {

	/**
	 * 
	 */
	private ZookeeperIMUserWriteManager imUserWriteManager;
	
	/**
	 * 这个imuser服务器的ip外网信息
	 */
	private String imuserIP;
	
	/**
	 * 这个imuser服务器的port信息
	 */
	private String imuserPort;
	
	/* (non-Javadoc)
	 * @see cn.org.wifit.core.zookeeper.NodeWriteDistributedTransactionWatcherHandle#doHandle(java.lang.String, java.lang.String)
	 */
	@Override
	public void doHandle(String mySequence, String eventFullpath) throws Exception {
		boolean isok = false;
		try {
			//先在本地节点初始化
			isok = this.imUserWriteManager.createLocalServerNodeTree();
			//然后再向zookeeper远程节点注册
			if(isok) {
				this.imUserWriteManager.createZookeeperNode(this.imuserIP, Integer.valueOf(this.imuserPort));
			}
			
			//最后建立当前IMUser和所有IMServer的连接
			this.imUserWriteManager.createIMServerTCPChannel();
		} catch(Exception e) {
			throw new RuntimeException(e);
		}
	}

	public ZookeeperIMUserWriteManager getImUserWriteManager() {
		return imUserWriteManager;
	}

	public void setImUserWriteManager(ZookeeperIMUserWriteManager imUserWriteManager) {
		this.imUserWriteManager = imUserWriteManager;
	}

	public String getImuserIP() {
		return imuserIP;
	}

	public void setImuserIP(String imuserIP) {
		this.imuserIP = imuserIP;
	}

	public String getImuserPort() {
		return imuserPort;
	}

	public void setImuserPort(String imuserPort) {
		this.imuserPort = imuserPort;
	}
}